package config

import (
	"io/ioutil"
	"log"
	"time"
	"net"
)

//ResultCode
const (
	OK        = 0  //成功
	Err       = -1 //错误
	OutOfDate = -2 //登陆过期
)
const (
	Debug = true //是否是调试
)

var LocalIP = ""

func GetIP() (string, error) {
	timeout := time.Nanosecond * 30
	conn, err := net.DialTimeout("tcp", "ns1.dnspod.net:6666", timeout*time.Second)
	defer func() {
		if x := recover(); x != nil {
			log.Println("Can't get public ip", x)
		}
		if conn != nil {
			conn.Close()
		}
	}()
	if err == nil {
		var bytes []byte
		deadline := time.Now().Add(timeout * time.Second)
		err = conn.SetDeadline(deadline)
		if err != nil {
			return "", err
		}
		bytes, err = ioutil.ReadAll(conn)
		if err == nil {
			return string(bytes), nil
		}
	}
	return "", err
}
